<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt"%>
<%@page import="com.yaodian100.core.common.utils.ServerValue"%>
<c-rt:set var="midContextPath" value='<%=ServerValue.getMidContextPath()%>'/>
<s:actionerror/>
<s:actionmessage/>
<s:fielderror />
<script language="javascript">
$().ready(function(){
	$.formValidator.initConfig({formid:"addressBookForm",onerror:function(msg){alert(msg)},onsuccess:function(){return true;}});
	$("#zipCode").numeric();
	$("#mobile").numeric();
	$("#areaCode").numeric();
	$("#phone").numeric();
	$("#extension").numeric();
	
	function commonTemplate(item) {
		return "<option value='" + item.Value  + "'>" + item.Text + "</option>"; 
	};
	function commonMatch(selectedValue) {
		return this.When == selectedValue; 
	};
  $.each(provinceArray,function(index, item){
    $("#province").append("<option value='" + item.Value + "'>" + item.Text + "</option>");
  });
	jQuery("#city").cascade("#province",{					
		list : cityArray,				
		template: commonTemplate,
		match: commonMatch 
	});
	jQuery("#district").cascade("#city",{					
		list: districtArray,
		template: commonTemplate,
		match: commonMatch 
	});
	
	if ( $("#id").val() != "" ){
		$.each(provinceArray,function(index, item){
			if ( item.Value == "${addressBook.province}" ){
				$("#province").attr("selectedIndex",index+1);
			}
		});
		$.each(cityArray,function(index, item){
			if ( item.When == "${addressBook.province}" ){
				$("#city").append("<option value='" + item.Value + "'>" + item.Text + "</option>");
			}
		});
		
		var newCityArray = jQuery.grep(cityArray, function (item) { return item.When == "${addressBook.province}"; });
		$.each(newCityArray,function(index, item){
			if ( item.Value == "${addressBook.city}" ){
				$("#city").attr("selectedIndex",index+1);
			}
		});
		
		$.each(districtArray,function(index, item){
			if ( item.When == "${addressBook.city}" ){
				$("#district").append("<option value='" + item.Value + "'>" + item.Text + "</option>");
			}
		});
		
		newDistrictArray = jQuery.grep(districtArray, function (item) { return item.When == "${addressBook.city}"; });
		$.each(newDistrictArray,function(index, item){
			if ( item.Value == "${addressBook.district}" ){
				$("#district").attr("selectedIndex",index+1);
			}
		});
	}
	$("#contactName").formValidator({onfocus:"请输入联络人姓名,请勿输入特殊字元如:!@#$%^&*()_+-*/",oncorrect:"联络人姓名格式正确"}).inputValidator({min:2,max:16,onerror:"联络人姓名须介于2-16个字元之间不能为空值"});
	$("#zipCode").formValidator({onfocus:"请输入邮递区号",oncorrect:"邮递区号格式正确"}).inputValidator({min:3,max:6,onerror:"邮递区号须介于3-6个字元之间不能为空值"});
	$("#addr").formValidator({onfocus:"請輸入地址",oncorrect:"地址格式輸入正確"}).inputValidator({min:2,max:100,onerror:"地址须介于2-100个字元之间且不能为空值"});
	$("#submitBtn").click(function(){
		if ( $("#province option:selected").val() == "" ){
			alert("请选择省");
			return false;
		}
		if ( $("#city option:selected").val() == "" ){
			alert("请选择城市");
			return false;
		}
		if ( $("#district option:selected").val() == "" ){
			alert("请选择地区");
			return false;
		}
		if ( $("#addr").val() == "" ){
			alert("请输入地址");
			return false;
		}
		
		$.post("${midContextPath}/member/addressBook!save.do", $('#addressBookForm').serialize(), callbackFun, "json");

		function callbackFun(saveResult){
			if ( saveResult == "" ){
				closeDialog();
				doAR('${midContextPath}/member/addressBook!search.do?AJAX=true&refreshZone=tabs-1&memberId=${memberId}','tabs-1');
			}else
				alert(saveResult);
		}
	});
});
</script>
<s:form action="addressBook!save.do" method="post" id="addressBookForm">
	<table class="tb-data">
		<tr>
			<th>联络人姓名</th>
			<td>
		    <input type="text" name="contactName" id="contactName" value="${addressBook.contactName}" maxlength="16"/>
		  	<div id="contactNameTip" style="width:450px;"></div>
	  	</td>
	  </tr>
  	<tr>
			<th>地址</th>
			<td>邮递区号
			<input type="text" name="zipCode" id="zipCode" value="${addressBook.zipCode}" maxlength="10"/>
	    <div id="zipCodeTip" style="width:450px;"></div>
	    <select id="province" name="province">
				<option value="">== 选择省 ==</option>
			</select>省
			<select id="city" name="city">
				<option value="">== 选择城市 ==</option>
			</select>城市
			<select id="district" name="district">
				<option value="">== 选择地区 ==</option>
			</select>地区<br/>
			<input type="text" name="addr" id="addr" value="${addressBook.addr}" maxlength="100"/>
			<div id="addrTip" style="width:450px;"></div>
	  	</td>
  	</tr>
		<tr>
			<th>手机</th>
    	<td><input type="text" name="mobile" id="mobile" value="${addressBook.mobile}" maxlength="20"/></td>
  	</tr>
  	<tr>
			<th>电话</th>
			<td>
		    <input type="text" name="areaCode" id="areaCode" value="${addressBook.areaCode}" maxlength="10"/>-
		    <input type="text" name="phone" id="phone" value="${addressBook.phone}" maxlength="20"/>
 				分机
 				<input type="text" name="extension" id="extension" value="${addressBook.extension}" maxlength="10"/>
 			</td>
  	</tr>
  	<tr>
  		<th>备注</th>
  		<td><textarea rows="3" cols="70" id="memo" name="memo">${addressBook.memo}</textarea></td>
  	</tr>   
  	<tr>
  		<td colspan="2">
	  		<input type="hidden" name="memberId" value="${memberId}"/>
				<input type="hidden" name="id" value="${addressBook.id}"/>
				<input type="button" id="submitBtn" value="送出"/>
				<input type="button" id="cancelBtn" value="Cancel"/>
			</td>
		</tr>
	</table>
</s:form>